package org.gdstash.ui.table;

import org.gdstash.db.*;
import org.gdstash.item.GDItem;
import org.gdstash.util.GDMsgFormatter;

import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class GDItemSetInfo implements Comparable<GDItemSetInfo> {
    private DBItemSet dbItemSet;
    private int itemSetLevel;
    private String itemSetRarity;
    private List<GDItemInfo> itemInfos;
    private List<DBItemCraft> dbItemCrafts;
    private List<GDItemInfo> bpInfos;
    private boolean scComplete;
    private boolean hcComplete;

    public static List<GDItemSetInfo> buildSetInfo() {
        final List<GDItemSetInfo> list = new LinkedList<GDItemSetInfo>();
        final List<DBItemSet> sets = DBItemSet.getAll();
        for (final DBItemSet set : sets) {
            final GDItemSetInfo setInfo = new GDItemSetInfo(set);
            list.add(setInfo);
        }
        Collections.<GDItemSetInfo>sort(list, new ItemSetInfoComparator());
        return list;
    }

    private GDItemSetInfo(final DBItemSet set) {
        this.dbItemSet = set;
        final List<String> itemIDs = DBItemSetAlloc.getByItemSetID(set.getItemSetID());
        final List<GDItem> setItems = DBItem.getGDItemsByItemIDs(itemIDs);
        this.dbItemCrafts = new LinkedList<DBItemCraft>();
        final List<GDItem> blueprints = new LinkedList<GDItem>();
        final List<GDItem> uniqueBPs = new LinkedList<GDItem>();
        for (final String itemID : itemIDs) {
            final DBItemCraft ic = DBItemCraft.getByCraftID(itemID);
            if (ic != null) {
                this.dbItemCrafts.add(ic);
                final List<GDItem> bps = DBStashItem.getGDItemsByItemID(ic.getItemID());
                if (bps == null || bps.isEmpty()) {
                    continue;
                }
                blueprints.addAll(bps);
                uniqueBPs.add(bps.get(0));
            }
        }
        if (!setItems.isEmpty()) {
            final GDItem gdi = setItems.get(0);
            this.itemSetLevel = gdi.getRequiredLevel();
            this.itemSetRarity = gdi.getRarity();
        }
        final List<GDItem> items = DBStashItem.getGDItemsByItemIDs(itemIDs);
        this.itemInfos = GDItemInfo.buildItemInfo(setItems, items);
        this.bpInfos = GDItemInfo.buildItemInfo(uniqueBPs, blueprints);
        Collections.<GDItemInfo>sort(this.itemInfos, new ItemInfoComparator());
        this.assignBlueprints();
    }

    private void assignBlueprints() {
        if (this.bpInfos == null) {
            return;
        }
        for (final GDItemInfo bpInfo : this.bpInfos) {
            for (final DBItemCraft dbItemCraft : this.dbItemCrafts) {
                if (dbItemCraft.getItemID().equals(bpInfo.gdItem.getItemID())) {
                    for (final GDItemInfo itemInfo : this.itemInfos) {
                        if (dbItemCraft.getCraftID().equals(itemInfo.gdItem.getItemID())) {
                            if (bpInfo.scCount > 0) {
                                itemInfo.scBlueprint = bpInfo.gdItem;
                            }
                            if (bpInfo.hcCount > 0) {
                                itemInfo.hcBlueprint = bpInfo.gdItem;
                                break;
                            }
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }

    private void determineCompleteness() {
        this.scComplete = true;
        this.hcComplete = true;
        for (final GDItemInfo itemInfo : this.itemInfos) {
            if (itemInfo.scCount <= 0 && itemInfo.scBlueprint == null) {
                this.scComplete = false;
            }
            if (itemInfo.hcCount <= 0 && itemInfo.hcBlueprint == null) {
                this.hcComplete = false;
            }
            if (!this.scComplete && !this.hcComplete) {
                break;
            }
        }
    }

    public String getItemSetID() {
        return this.dbItemSet.getItemSetID();
    }

    public String getItemSetName() {
        return this.dbItemSet.getName();
    }

    public GDItem.LabelInfo getItemSetNameLabel() {
        final GDItem.LabelInfo label = new GDItem.LabelInfo();
        label.text = this.getItemSetName();
        label.foreground = ItemClass.getRarityColor(this.itemSetRarity);
        return label;
    }

    public int getItemSetLevel() {
        return this.itemSetLevel;
    }

    public GDItem.LabelInfo getItemSetLevelLabel() {
        final GDItem.LabelInfo label = new GDItem.LabelInfo();
        label.text = Integer.toString(this.getItemSetLevel());
        label.foreground = ItemClass.getRarityColor(this.itemSetRarity);
        return label;
    }

    public String getItemSetRarity() {
        return this.itemSetRarity;
    }

    public GDItem.LabelInfo getItemSetRarityLabel() {
        final GDItem.LabelInfo label = new GDItem.LabelInfo();
        label.text = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "RARITY_ITEM_COMMON");
        if (this.itemSetRarity.equals("Magical")) {
            label.text = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "RARITY_ITEM_MAGICAL");
        }
        if (this.itemSetRarity.equals("Rare")) {
            label.text = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "RARITY_ITEM_RARE");
        }
        if (this.itemSetRarity.equals("Epic")) {
            label.text = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "RARITY_ITEM_EPIC");
        }
        if (this.itemSetRarity.equals("Legendary")) {
            label.text = GDMsgFormatter.getString(GDMsgFormatter.rbGD, "RARITY_ITEM_LEGENDARY");
        }
        label.foreground = ItemClass.getRarityColor(this.itemSetRarity);
        return label;
    }

    public int getItemSetRarityInt() {
        return ItemClass.getRarityInt(this.itemSetRarity);
    }

    public List<GDItemInfo> getItemInfoList() {
        return this.itemInfos;
    }

    public int getMaxImageHeight() {
        int height = 0;
        for (final GDItemInfo info : this.itemInfos) {
            final BufferedImage img = info.getFullImage();
            if (img != null && height < img.getHeight()) {
                height = img.getHeight();
            }
        }
        return height;
    }

    @Override
    public int compareTo(final GDItemSetInfo info) {
        if (!this.itemSetRarity.equals(info.itemSetRarity)) {
            if (this.itemSetRarity.equals("Legendary")) {
                return 1;
            }
            return -1;
        } else {
            int i = 0;
            i = this.getItemSetName().compareTo(info.getItemSetName());
            if (i != 0) {
                return i;
            }
            i = this.itemSetLevel - info.itemSetLevel;
            if (i != 0) {
                return i;
            }
            return 0;
        }
    }

    private static class ItemInfoComparator implements Comparator<GDItemInfo> {
        @Override
        public int compare(final GDItemInfo ii1, final GDItemInfo ii2) {
            final int i1 = ItemClass.getClassInt(ii1.gdItem.getItemClass());
            final int i2 = ItemClass.getClassInt(ii2.gdItem.getItemClass());
            return i1 - i2;
        }
    }

    private static class ItemSetInfoComparator implements Comparator<GDItemSetInfo> {
        @Override
        public int compare(final GDItemSetInfo is1, final GDItemSetInfo is2) {
            final int iRarity = is1.getItemSetRarityInt() - is2.getItemSetRarityInt();
            if (iRarity != 0) {
                return iRarity;
            }
            final int iName = is1.dbItemSet.getName().compareTo(is2.dbItemSet.getName());
            if (iName != 0) {
                return iName;
            }
            return is1.getItemSetLevel() - is2.getItemSetLevel();
        }
    }
}
